/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/*
 * @author Hany
 */
package pucit;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.sql.*;
import java.util.Hashtable;

public class rentacarDAO {

    private Connection con;

    private void connect() throws ClassNotFoundException, SQLException {
        Class.forName("com.mysql.jdbc.Driver");
        String url = "jdbc:mysql://127.0.0.1/rentacar";
        con = DriverManager.getConnection(url, "root", "root");
    }

    //Process the HTTP Get request
    public boolean SignIn(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {
        //    response.setContentType("text/html");
//        PrintWriter out = response.getWriter();

        String username = request.getParameter("username").toString();
        String password = request.getParameter("password").toString();
        //out.println(user_name + " " + pass_word);
        try {

            connect();

            Statement st = con.createStatement();

            String query = "Select * from user_data where username='" + username + "' AND password ='" + password + "';";

            ResultSet rs = st.executeQuery(query);

            if (rs.next()) {
                request.getSession().setAttribute("username", username);
                String name = rs.getString("name") + " " + rs.getString("lastname");
                request.getSession().setAttribute("name", name);
                return true;
            } else {

                return false;
            }

        } catch (Exception e) {

            //          out.println(e);

            return false;
        } finally {
            con.close();

        }

    }

    public boolean SignUp(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {

        String username = request.getParameter("uname").toString();
        String password = request.getParameter("pwd").toString();
        String name = request.getParameter("fname").toString();
        String lastname = request.getParameter("lname").toString();
        String address = request.getParameter("address").toString();
        String city = request.getParameter("city").toString();
        String country = request.getParameter("country").toString();
        String email = request.getParameter("email").toString();
        String profession = request.getParameter("spec").toString();
        String phone = request.getParameter("phone").toString();

        //out.println(user_name + " " + pass_word);

        try {

            connect();
            String query = "INSERT INTO USER_DATA(username, password, name, lastName, profession, address, city, country, phone, email) VALUES(?,?,?,?,?,?,?,?,?,?)";
            PreparedStatement ps = con.prepareStatement(query);

            ps.setString(1, username);
            ps.setString(2, password);
            ps.setString(3, name);
            ps.setString(4, lastname);
            ps.setString(5, profession);
            ps.setString(6, address);
            ps.setString(7, city);
            ps.setString(8, country);
            ps.setString(9, phone);
            ps.setString(10, email);

            int result = ps.executeUpdate();

            if (result > 0) {
                return true;
            } else {
                return false;
            }

        } catch (Exception e) {
            return false;
        } finally {
            con.close();

        }
    }

   
     public boolean isAvailable(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String username = request.getParameter("username");

        connect();
        String query = "Select * from user_data where username='" + username + "';";
        Statement st = con.createStatement();
        ResultSet rs = st.executeQuery(query);

        if (rs.next()) {
            return false;
        } else {
            return true;
        }

    }
     
     public Hashtable<String,String> getDetails(String username) throws Exception {
       
        connect();
        String query = "Select * from user_data where username='" + username + "';";
        Statement st = con.createStatement();
        ResultSet rs = st.executeQuery(query);

        if (rs.next()) {
            Hashtable<String,String> values=new Hashtable<String,String>();
            values.put("name", rs.getString("name").toString());
             values.put("lastname", rs.getString("lastname").toString());
              values.put("address", rs.getString("address").toString());
               values.put("city", rs.getString("city").toString());
                values.put("phone", rs.getString("phone").toString());
                 values.put("email", rs.getString("email").toString());
                  values.put("profession", rs.getString("profession").toString());
                   values.put("country", rs.getString("country").toString());
                   return values;
        } else {
            return null;
        }

    }
     
       public boolean update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {

        String username = request.getSession().getAttribute("username").toString();
        String name = request.getParameter("fname").toString();
        String lastname = request.getParameter("lname").toString();
        String address = request.getParameter("address").toString();
        String city = request.getParameter("city").toString();
        String country = request.getParameter("country").toString();
        String email = request.getParameter("email").toString();
        String profession = request.getParameter("spec").toString();
        String phone = request.getParameter("phone").toString();

        //out.println(user_name + " " + pass_word);

        try {

            connect();
            String query = "update USER_DATA set name=?, lastName=?, profession=?, address=?, city=?, country=?, phone=?, email=? where username=?";
            PreparedStatement ps = con.prepareStatement(query);

      
            ps.setString(1, name);
            ps.setString(2, lastname);
            ps.setString(3, profession);
            ps.setString(4, address);
            ps.setString(5, city);
            ps.setString(6, country);
            ps.setString(7, phone);
            ps.setString(8, email);
            
            ps.setString(9, username);

            int result = ps.executeUpdate();

            if (result > 0) {
                return true;
            } else {
                return false;
            }

        } catch (Exception e) {
            return false;
        } finally {
            con.close();

        }
    }
       
         public boolean updatepassword(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {

        String username = request.getSession().getAttribute("username").toString();
        String oldPass =  request.getParameter("oldpass").toString();
        String password = request.getParameter("newpass").toString();
        //out.println(user_name + " " + pass_word);

        try {

            connect();
            String query = "update USER_DATA set password=? where username=? and password=?";
            PreparedStatement ps = con.prepareStatement(query);

      
            ps.setString(1, password);
            ps.setString(2, username);
            ps.setString(3, oldPass);

            int result = ps.executeUpdate();

            if (result > 0) {
                return true;
            } else {
                return false;
            }

        } catch (Exception e) {
            return false;
        } finally {
            con.close();

        }
    }
    
    
}

